// ==UserScript==
// @name         18COMIC优化
// @namespace    https://gitcode.com/gagghik1
// @homepage     https://gitcode.com/gagghik1/greyz4k
// @version      2.0.0
// @description  
// @author       琳酱
// @match        *://18comic.vip/*
// @match        *://18comic.ink/*
// @match        *://jmcomic-zzz.one/*
// @match        *://jmcomic-zzz.org/*
// @icon         https://raw.gitcode.com/gagghik1/greyz4k/raw/main/18comic/icon.png
// @grant        none
// @run-at       document-start
// @downloadURL  https://raw.gitcode.com/gagghik1/greyz4k/raw/main/18comic/user.js
// @updateURL    https://raw.gitcode.com/gagghik1/greyz4k/raw/main/18comic/meta.js
// ==/UserScript==

(function () {
    'use strict';

    // 样式覆盖div-bf-pv高度
    const style = document.createElement('style');
    style.textContent = `
        .div-bf-pv {
            height: 20px !important;
            min-height: 20px !important;
            max-height: 20px !important;
            overflow: hidden !important;
        }
    `;
    document.head.appendChild(style);

    const selectorsToRemove = [
        '#billboard-modal',
        '.ae65e2-f_1_sticky2',
        '.ae65e2-f_1',
        '.group-notice',
        '.float_right',
        '.hidden-lg[style*="bottom: 0px"]',
        '.modal-backdrop.fade.in',
        '#guide-modal'
    ];

    function removeAdsAndModals() {
        selectorsToRemove.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => el.remove());
        });

        if (document.body.classList.contains('modal-open')) {
            document.body.classList.remove('modal-open');
            document.body.style.overflow = 'auto';
        }
    }

    function clearDivBfPv() {
        const el = document.querySelector('.div-bf-pv');
        if (el) {
            el.innerHTML = '';
        }
    }

    // 自动点击“确定进入”按钮，关闭引导弹窗
    function autoClickGuide() {
        const btn = document.querySelector('#chk_guide');
        if (btn) {
            btn.click();
        }
    }

    function doClean() {
        removeAdsAndModals();
        clearDivBfPv();
        autoClickGuide();
    }

    const readyInterval = setInterval(() => {
        if (document.body) {
            doClean();

            const observer = new MutationObserver(doClean);
            observer.observe(document.body, { childList: true, subtree: true });

            clearInterval(readyInterval);
        }
    }, 10);

    setInterval(doClean, 1000);

})();
